<template>
  <div v-if="detail">
    <el-dialog title="订单取消" :visible.sync="dialogVisible" width="600px" @close="close">
      <el-form
        ref="form"
        label-suffix="："
        :model="form"
        label-width="100px"
        :rules="rules"
        :close-on-click-modal="false"
        size="small"
      >
        <el-form-item label="订单编号">{{ detail.order.orderNo }}</el-form-item>
        <el-form-item label="下单时间">{{ detail.order.createTime }}</el-form-item>
        <el-form-item label="下单顾客">{{ (detail.customer && detail.customer.name) || "-" }}</el-form-item>
        <!-- <el-form-item label="支付金额">￥{{ detail.order.payFee }}</el-form-item>
        <el-form-item label="退款金额">￥{{ detail.order.payFee }}</el-form-item> -->
        <el-form-item label="退款备注" >
          <el-input v-model.trim="form.remark" maxlength="50" />
          <span style="color:red;">*确认取消将向会员原路退回实收金额{{ detail.order.payFee }}元余额</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" @click="openInnerdialog">退款</el-button>
      </div>

      <!--支付密码弹框-->
      <!-- <el-dialog
        title="验证身份"
        :visible.sync="dialogVisibleInset"
        width="500px"
        append-to-body
        :close-on-click-modal="false"
      >
        密码输入框
        <div
          v-if="dialogVisibleInset"
          v-loading="loading"
          element-loading-text="退款中"
          class="dialog-inset"
        >
          <div class="refund-num">
            <div>退款金额</div>
            <div>￥{{ detail.order.payFee }}</div>
          </div>
          <div class="refund-psw">
            <PassWordBox ref="passWordBox" @sendPsw="sendPsw" />
          </div>
        </div>
      </el-dialog> -->
    </el-dialog>
     <el-dialog
      title="验证身份"
      :visible.sync="dialogVisibleInset"
      append-to-body
      width="40%"
      :before-close="pwdClose"
    >
      <el-form ref="form" :model="refundForm" label-width="80px">
        <el-form-item label="登录密码">
          <el-input v-model="refundForm.pwd" size="small" placeholder="请输入登录密码" />
          <span>*验证登录密码成功后会直接发起退款</span>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="pwdClose()">取消</el-button>
          <el-button type="primary" size="small" @click="submit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { refundOrder } from '../../api/record'
import {chkLoginPwd} from '@/api/payCenter'
export default {
  data() {
    return {
      /**
         * 校验字段相关
         */
      rules: {
        remark: [
          {
            required: true,
            message: '请输入退款备注'
          }
        ]
      },
      // 数据
      dialogVisibleInset: null,
      // 配置
      resolve: null,
      reject: null,
      dialogVisible: null,
      loading: false,
      // 数据
      detail: '',
      form: {
        deviceId: '',
        operatorId: '',
        orderNo: '',
        refundPwd: '',
        remark: ''
      },
      refundForm:{
        pwd:''
      }
    }
  },
  methods: {
    open(detail) {
      this.detail = detail
      this.form.orderNo = detail.order.orderNo
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    pwdClose(done) {
      this.dialogVisibleInset = false
      this.refundForm.pwd = ''
      if (done) done()
    },
    submit() {
      const { pwd } = this.refundForm
      // const { pluginCode, status } = this.dataForm
      if (!pwd) {
        this.$message.error('请输入登录密码')
        return
      }
      // 校验登录密码
      chkLoginPwd({ password: pwd }).then(res => {
        if(res.code == 0){
          this.sendPsw()
        }
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.form = {
        deviceId: '',
        operatorId: '',
        orderNo: '',
        refundPwd: '',
        remark: ''
      }
      this.loading = false
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    // 打开密码弹框
    openInnerdialog(formName) {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = false
          this.dialogVisibleInset = true
        }
      })
    },

    // 提交退款
    sendPsw() {
      const params = Object.assign({}, this.form)
      const { pwd } = this.refundForm
      // let md5Password = this.$md5(String(psw));
      params.refundPwd = pwd
      this.loading = true
      refundOrder(params)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '退款成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          // this.$refs.passWordBox.defaultPsw()
          this.dialogVisibleInset = false
          this.resolve && this.resolve()
          this.hide()
        })
        .catch(err => {
          this.refundForm.pwd = ''
          // this.$refs.passWordBox.defaultPsw()
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  /* 内层弹框 */
  .dialog-inset {
    /* 退款金额*/
    .refund-num {
      text-align: center;
      div:first-child {
        padding: 20px 0;
        font-size: 18px;
      }
      div:last-child {
        font-size: 30px;
      }
    }
    /* 密码输入框 */
    .refund-psw {
      padding: 20px 0;
    }
  }
</style>
